package com.haixiaoke.saas.repository.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.repository.dto.MetroStationDTO;
import com.haixiaoke.saas.repository.mapper.MetroStationMapper;
import com.haixiaoke.saas.repository.pojo.MetroLineStation;
import com.haixiaoke.saas.repository.pojo.MetroStation;
import com.haixiaoke.saas.repository.service.IMetroLineStationService;
import com.haixiaoke.saas.repository.service.IMetroStationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 地铁车站Service业务层处理
 *
 * @author haixiaoke
 * @date 2023-04-07
 */
@Service
public class MetroStationServiceImpl implements IMetroStationService {
    @Autowired
    private MetroStationMapper metroStationMapper;
    @Autowired
    private IMetroLineStationService metroLineStationService;

    /**
     * 查询地铁车站
     *
     * @param stationId 地铁车站主键
     * @return 地铁车站
     */
    @Override
    public MetroStation selectMetroStationByStationId(Long stationId) {
        return metroStationMapper.selectMetroStationByStationId(stationId);
    }

    @Override
    public List<MetroStation> selectMetroStationByStationIdIn(List<Long> stationIds) {
        return metroStationMapper.selectMetroStationByStationIdIn(stationIds);
    }

    /**
     * 查询地铁车站列表
     *
     * @param metroStation 地铁车站
     * @return 地铁车站
     */
    @Override
    public List<MetroStation> selectMetroStationList(MetroStation metroStation) {
        return metroStationMapper.selectMetroStationList(metroStation);
    }

    /**
     * 新增地铁车站
     *
     * @param metroStationDTO 地铁车站
     * @return 结果
     */
    @Override
    @Transactional
    public int insertMetroStation(MetroStationDTO metroStationDTO) {

        int result = 0;
        MetroStation metroStation;
        if (metroStationMapper.checkStationUniqueness(metroStationDTO.getStationName(), metroStationDTO.getProvinceId(), metroStationDTO.getCityId()) == 0) {
            metroStation = new MetroStation();
            metroStationDTO.setCreateTime(new Date());
            BeanUtil.copyProperties(metroStationDTO, metroStation);
            //新增站台
            result = metroStationMapper.insertMetroStation(metroStation);
        } else {
            metroStation = metroStationMapper.selectMetroStation(metroStationDTO.getStationName(), metroStationDTO.getProvinceId(), metroStationDTO.getCityId());
        }

        MetroLineStation po = new MetroLineStation();
        po.setStationId(metroStation.getStationId());
        po.setStationName(metroStation.getStationName());
        po.setLineId(metroStationDTO.getLineId());
        po.setLineName(metroStationDTO.getLineName());
        po.setSort(metroStationDTO.getSort());

        //批量新增线路和站台关联表
        metroLineStationService.insertMetroLineStation(po);

        return result;
    }

    /**
     * 修改地铁车站
     *
     * @param metroStation 地铁车站
     * @return 结果
     */
    @Override
    public int updateMetroStation(MetroStation metroStation) {
        metroStation.setUpdateTime(new Date());
        return metroStationMapper.updateMetroStation(metroStation);
    }

    /**
     * 批量删除地铁车站
     *
     * @param stationIds 需要删除的地铁车站主键
     * @return 结果
     */
    @Override
    public int deleteMetroStationByStationIds(Long[] stationIds) {
        return metroStationMapper.deleteMetroStationByStationIds(stationIds);
    }

    /**
     * 删除地铁车站信息
     *
     * @param stationId 地铁车站主键
     * @return 结果
     */
    @Override
    public int deleteMetroStationByStationId(Long stationId) {
        return metroStationMapper.deleteMetroStationByStationId(stationId);
    }

    /**
     * 校验经纬度是否存在
     *
     * @param longitude 经度
     * @param latitude  纬度
     * @return
     */
    @Override
    public boolean checkLongitudeAndLatitude(Long longitude, Long latitude) {
        return metroStationMapper.checkLongitudeAndLatitude(longitude, latitude) != 0;
    }

    @Override
    public boolean checkStationUniqueness(String stationName, Long provinceId, Long cityId) {
        return metroStationMapper.checkStationUniqueness(stationName, provinceId, cityId) != 0;
    }

    @Override
    public int softDeleteMetroStationByStationIds(Long[] stationIds) {
        return metroStationMapper.softDeleteMetroStationByStationIds(stationIds);
    }

    @Override
    public String selectMetroStationNamesByStationIdIn(List<Long> stationIds) {
        List<MetroStation> metroStationList = this.selectMetroStationByStationIdIn(stationIds);
        StringBuilder metroStationNames = new StringBuilder();
        if (!metroStationList.isEmpty()){
            metroStationList.forEach(m->metroStationNames.append(m.getStationName()).append(','));
        }
        if (StrUtil.isNotBlank(metroStationNames)) {
            metroStationNames.deleteCharAt(metroStationNames.lastIndexOf(","));
        }
        return metroStationNames.toString();
    }

}
